﻿/* System.Compute: a Computation Framework for .NET/Mono
 * Copyright (C) 2011 Ralf "raboni" Boeckhorst
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 */

using System.Runtime.InteropServices;
using System.Text;

namespace System.Compute.OpenCL
{
    public sealed class Platform
    {
        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clGetPlatformIDs(SizeT num_entries, IntPtr[] platforms, ref SizeT num_platforms);

        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clGetPlatformIDs(SizeT num_entries, IntPtr[] platforms, SizeT num_platforms);

        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clGetPlatformInfo(IntPtr platform, PlatformInfo param_name, SizeT param_value_size, IntPtr param_value, ref SizeT param_value_size_ret);

        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clGetPlatformInfo(IntPtr platform, PlatformInfo param_name, SizeT param_value_size, byte[] param_value, SizeT param_value_size_ret);

        internal IntPtr Handle;

        public string Profile
        {
            get
            {
                SizeT param_value_size = 0;
                ErrorCode res = clGetPlatformInfo(Handle, PlatformInfo.Profile, 0, IntPtr.Zero, ref param_value_size);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                byte[] param_value = new byte[(long)param_value_size];
                res = clGetPlatformInfo(Handle, PlatformInfo.Profile, param_value_size, param_value, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return Encoding.Default.GetString(param_value);
            }
        }

        public string Version
        {
            get
            {
                SizeT param_value_size = 0;
                ErrorCode res = clGetPlatformInfo(Handle, PlatformInfo.Version, 0, IntPtr.Zero, ref param_value_size);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                byte[] param_value = new byte[(long)param_value_size];
                res = clGetPlatformInfo(Handle, PlatformInfo.Version, param_value_size, param_value, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return Encoding.Default.GetString(param_value);
            }
        }

        public string Name
        {
            get
            {
                SizeT param_value_size = 0;
                ErrorCode res = clGetPlatformInfo(Handle, PlatformInfo.Name, 0, IntPtr.Zero, ref param_value_size);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                byte[] param_value = new byte[(long)param_value_size];
                res = clGetPlatformInfo(Handle, PlatformInfo.Name, param_value_size, param_value, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return Encoding.Default.GetString(param_value);
            }
        }

        public string Vendor
        {
            get
            {
                SizeT param_value_size = 0;
                ErrorCode res = clGetPlatformInfo(Handle, PlatformInfo.Vendor, 0, IntPtr.Zero, ref param_value_size);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                byte[] param_value = new byte[(long)param_value_size];
                res = clGetPlatformInfo(Handle, PlatformInfo.Vendor, param_value_size, param_value, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return Encoding.Default.GetString(param_value);
            }
        }

        public string Extensions
        {
            get
            {
                SizeT param_value_size = 0;
                ErrorCode res = clGetPlatformInfo(Handle, PlatformInfo.Extensions, 0, IntPtr.Zero, ref param_value_size);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                byte[] param_value = new byte[(long)param_value_size];
                res = clGetPlatformInfo(Handle, PlatformInfo.Extensions, param_value_size, param_value, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return Encoding.Default.GetString(param_value);
            }
        }

        internal Platform(IntPtr handle)
        {
            Handle = handle;
        }

        public static Platform[] GetPlatforms()
        {
            SizeT num_platforms = 0;
            ErrorCode res = clGetPlatformIDs(0, null, ref num_platforms);
            if (res != ErrorCode.Success)
                throw new OpenCLException(res);
            IntPtr[] platforms = new IntPtr[(long)num_platforms];
            res = clGetPlatformIDs(num_platforms, platforms, 0);
            if (res != ErrorCode.Success)
                throw new OpenCLException(res);
            Platform[] arr = new Platform[(long)num_platforms];
            for (long i = 0; i < num_platforms; i++)
            {
                arr[i] = new Platform(platforms[i]);
            }
            return arr;
        }
    }
}
